﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Configuration;

namespace Project.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public override void Init()
        {
            base.Init();
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            string s = System.Configuration.ConfigurationManager.AppSettings["Domain"];

            if (String.IsNullOrEmpty(s) == false)
            {
                string[] domain = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                
                routes.Add("default project", new PublicRoute(
                  new { controller = "Home", action = "Index" },
                  new string[] { "Project.Module.Controller"},
                  domain));
            }
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }

    public class PublicRoute : Route
    {
        #region Constructors

        #region default

        public PublicRoute(string url, IRouteHandler routeHandler)
            : base(url, routeHandler)
        {

        }

        public PublicRoute(object defaults)
            : base(string.Empty, new RouteValueDictionary(defaults), new MvcRouteHandler())
        {
        }

        public PublicRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
            : base(url, defaults, routeHandler)
        {
        }
        public PublicRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
            : base(url, defaults, constraints, routeHandler)
        {
        }
        public PublicRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
            : base(url, defaults, constraints, dataTokens, routeHandler)
        {
        }

        #endregion

        private string[] _namespaces = new string[] { };
        private string[] _domains = new string[] { };

        public PublicRoute(object defaults, string[] namespaces, string[] domains)
            : base(string.Empty, new RouteValueDictionary(defaults), new MvcRouteHandler())
        {
            this._namespaces = namespaces;
            this._domains = domains;
        }

        #endregion

        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            string rawurl = httpContext.Request.RawUrl;
            if (rawurl.ToLower().IndexOf("/content") == 0 || rawurl.ToLower().IndexOf("/controllers") == 0 ||
                rawurl.ToLower().IndexOf("/models") == 0 || rawurl.ToLower().IndexOf("/scripts") == 0 ||
                rawurl.ToLower().IndexOf("/views") == 0 || rawurl.ToLower().Contains("inetpub") == true )
            {
                return null;
            }

            RouteData data = new RouteData(this, this.RouteHandler);

            if (Defaults != null)
            {
                foreach (KeyValuePair<string, object> item in Defaults)
                {
                    data.Values[item.Key] = item.Value;
                }
            }

            data.DataTokens["Namespaces"] = this._namespaces;

            return data;
        }
    }
}